package com.flong.gui.notepad;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
 * 字体对话框
 * @author liangjilong
 *
 */
public class FontDialog extends JDialog implements ListSelectionListener, ActionListener
{
	private static final long serialVersionUID = 1L;
	private JPanel p, p1, p2, p3, p4, p5, p6;
	private JLabel j1, j2, j3, j4;
	private JTextField t1, t2, t3;
	private JList jl1, jl2, jl3;
	private JButton b1, b2;
	private Font font;
	private NotePad pad;
	
	public FontDialog(NotePad pad)
	{
		this.pad = pad;
		p = new JPanel();
		p.setLayout(new GridLayout(1, 3));
		
		p1 = new JPanel();
		p2 = new JPanel();
		p3 = new JPanel();
		p4 = new JPanel();
		
		j1 = new JLabel("字体");
		j2 = new JLabel("字形");
		j3 = new JLabel("大小");
		
		t1 = new JTextField(7);
		t2 = new JTextField(7);
		t3 = new JTextField(7);
		
		String[] font = {"宋体", "华文楷体", "华文宋体", "隶书", "华文中宋"};
		String[] style = {"常规", "粗体", "斜体", "粗斜体"};
		String[] size = {"8","16","24","32","48"};
		jl1 = new JList(font);
		jl1.setVisibleRowCount(4);
		jl1.setFixedCellWidth(62);
		JScrollPane sp1 = new JScrollPane(jl1);
		
		jl2 = new JList(style);
		jl2.setVisibleRowCount(4);
		jl2.setFixedCellWidth(75);
		JScrollPane sp2 = new JScrollPane(jl2);
		jl3 = new JList(size);
		jl3.setVisibleRowCount(4);
		jl3.setFixedCellWidth(62);
		
		JScrollPane sp3 = new JScrollPane(jl3);
		b1 = new JButton("确定");
		b2 = new JButton("取消");
		b1.addActionListener(this);
		b2.addActionListener(this);
		b1.setActionCommand("yes");
		b2.setActionCommand("no");
		
		jl1.setSelectedIndex(0);
		jl2.setSelectedIndex(0);
		jl3.setSelectedIndex(1);
		jl1.addListSelectionListener(this);
		jl2.addListSelectionListener(this);
		jl3.addListSelectionListener(this);
		
		t1.setText((String)jl1.getSelectedValue());
		t2.setText((String)jl2.getSelectedValue());
		t3.setText((String)jl3.getSelectedValue());
		
		p5 = new JPanel();
		p5.setLayout(new BorderLayout());
		p6 = new JPanel();
		j4 = new JLabel("示例字体");
		p6.add(j4);
		
		p1.add(j1);
		p1.add(t1);
		p1.add(sp1);
		
		p2.add(j2);
		p2.add(t2);
		p2.add(sp2);
		
		p3.add(j3);
		p3.add(t3);
		p3.add(sp3);
		
		p.add(p1);
		p.add(p2);
		p.add(p3);
		
		p4.add(b1);
		p4.add(b2);
		
		p5.add(p6, BorderLayout.CENTER);
		p5.add(p4, BorderLayout.SOUTH);
		
		add(p, BorderLayout.CENTER);
		add(p5, BorderLayout.SOUTH);
		setTitle("字体对话框");
		setSize(300, 280);
		initLocation();
		setResizable(false);
		setModal(true);
		setVisible(true);		
	}
	
	private void initLocation()
	{
		JFrame frame = pad.getFrame();
		Point point = frame.getLocation();
		this.setLocation(point.x + 100, point.y + 100);
	}
	
	public void valueChanged(ListSelectionEvent e)
	{
		String s1 = (String)jl1.getSelectedValue();
		t1.setText(s1);
		String s2 = (String)jl2.getSelectedValue();
		t2.setText(s2);
		String s3 = (String)jl3.getSelectedValue();
		t3.setText(s3);
		
		font = new Font(s1, jl2.getSelectedIndex(), Integer.parseInt(s3));
		j4.setFont(font);
	}

	public void actionPerformed(ActionEvent e)
	{
		String s = e.getActionCommand();
		
		if (s.equals("yes"))
		{
			pad.setFont(font);
			this.dispose();
		}
		else
		{
			this.dispose();
		}
	}
}
